<?php

namespace C\L;

use C\P\Publics;
use Phalcon\Loader;
use Phalcon\Events\Manager;
use Phalcon\Db\Adapter\Pdo\Mysql;
use Phalcon\Http\Response;

class App
{

    private $di;

    public function __construct()
    {
        $timeZone = date_default_timezone_get();
        if ($timeZone == 'UTC') {
            date_default_timezone_set('PRC');
        }
    }

    private function pubRegService($di)
    {
        $di->set('loader', function () {
            return new Loader();
        });

      // 注册命名空间
        $di['loader']
        ->registerNamespaces([
        'C\L' => CORE . '/library/',
        'C\P' => CORE . '/plugins/',
        'C\S' => CORE . '/services/',
        'C\M' => CORE . '/models/',
        'C\R' => CORE . '/resources/',
        ])
        ->register();

      //配置文件服务
        $di->set('config', function () {
            return new Config();
        });

      //系统消息
        $di->set('message', function () {
            return new Message();
        }, true);

      //事件捕捉
        $di->set('eventsManager', function () {
            return new Manager();
        });

      //缓存服务
        $di->set('cache', function () use ($di) {
            $config = $di['config']->get('redis');
            return new \C\P\Redis($config);
        });

      //数据库
        $di->set('db', function () use ($di) {
            return new Mysql($di['config']->get('db')->toArray());
        });

      //加载自定义服务
        $serviceConfig = $di['config']->get('services', true);
        foreach ($serviceConfig as $key => $value) {
            $di->set($key, $value);
        }

      //日志服务
        $di->set('log', function () use ($di) {
            return new Logs();
        });

        $di->set('public', function () {
            return new Publics();
        }, true);

        $di->set('que', function () {
            return new \Phalcon\Queue\Beanstalk([
            'host' => '10.0.0.11',
            'port' => '11300',
            ]);
        }, true);

        $this->di = $di;

        return true;
    }

    private function cliRegService($di)
    {
        $di['loader']->registerDirs([APP_PATH . '/tasks',]);
    }

    private function webRegService($di)
    {
      //控制视图
        $di->set('view', function () {
            return new \Phalcon\Mvc\View();
        });

      //路由
        $di->set('router', function () {
            $router = new \Phalcon\Mvc\Router();

            $router->add(
                '/:module/:controller/:action/:params',
                [
                'module'     => 1,
                'controller' => 2,
                'action'     => 3,
                'params'     => 4
                ]
            );

            $router->notFound([
            'module'     => 'api',
            'controller' => 'api',
            'action'     => 'index',
            ]);

            return $router;
        });

      //web日志
        $di->set('weblog', function () use ($di) {
            return $di['log']->set(APP_NAME . '_' . date('md') . '.log');
        }, true);

        $di->set('ssid', function () {
            return new Session();
        }, true);

        return true;
    }

    private function setJsonToPost()
    {
        $inputData = trim(file_get_contents('php://input'));
        if (substr($inputData, 0, 1) != '{') {
            return;
        }

        $inputData = json_decode($inputData, true);
        if (empty($inputData)) {
            $inputData = [];
        }

        $_POST = array_merge($_POST, $inputData);
    }


    public function web()
    {
        try {
            $this->setJsonToPost();

            //注册公共类服务
            $this->pubRegService(
                new \Phalcon\Di\FactoryDefault()
            );

            //注册web类服务
            $this->webRegService(
                $this->di
            );

            $this->di['weblog']->info(
                json_encode(['url' => $_SERVER['QUERY_STRING'], 'post' => $_POST], JSON_UNESCAPED_UNICODE)
            );

            //控制显示
            $web = new \Phalcon\Mvc\Application();
            $web->setDi(
                $this->di
            );
            //注册模块
            $web->registerModules(
                $this->di['config']->getModConf()
            );

            //监听服务服务
            $web->setEventsManager(
                $this->di['eventsManager']
            );

            $this->di['dispatcher']->setEventsManager(
                $this->di['eventsManager']
            );

            $listener = new Listener();
            $this->di['eventsManager']->attach('application', $listener);
            $this->di['eventsManager']->attach('dispatch', $listener);

            $response = $web->handle();
            $response->send();
        } catch (\Exception $e) {
            $error = [
                'code' => 0,
                'message' => $e->getMessage(),
            ];

            if (APP_MODE == 'dev' || 1) {
                $error['dev_show_msg'] = $e->getMessage();
            }
            
            return $this->response($error);
        }
    }

    public function cli($argv)
    {

        $this->pubRegService(
            new \Phalcon\Di\FactoryDefault\Cli()
        );

        $this->cliRegService(
            $this->di
        );

        $cli = new \Phalcon\Cli\Console(
            $this->di
        );

        if (empty($argv[1])) {
            $argv[1] = 'main';
        }

        if (empty($argv[2])) {
            $argv[2] = 'run';
        }

        try {
          // 处理传入的参数
            $cli->handle([
            'task' => $argv[1],
            'action' => $argv[2],
            'params' => array_slice($argv, 3),
            ]);
        } catch (\Exception $e) {
            echo $e->getMessage();
        }
    }

    private function response($data)
    {
        $json = json_encode($data, JSON_UNESCAPED_UNICODE);

        $response = new Response;
        $response->setStatusCode(200);
        $response->setHeader('Content-Type', 'application/json; charset=UTF-8');
        $response->setHeader("Access-Control-Allow-Origin", '*');
        $response->setHeader("Access-Control-Allow-Methods", 'GET,PUT,POST,DELETE,OPTIONS');
        $response->setHeader(
            "Access-Control-Allow-Headers",
            'Origin, X-Requested-With, Content-Range, Content-Type, Authorization'
        );
        $response->setHeader("Access-Control-Allow-Credentials", true);
        $response->setContent($json);

        $response->send();

        exit();
    }
}
